/*
 * PrintConfiguration.java
 *
 * Created on Apr 5, 2010, 4:31:47 PM
 */
package com.floreantpos.config.ui;

import java.awt.Component;

import javax.print.PrintService;
import javax.print.PrintServiceLookup;
import javax.swing.DefaultComboBoxModel;
import javax.swing.DefaultListCellRenderer;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JList;

import com.floreantpos.config.ApplicationConfig;
import com.floreantpos.config.PrintConfig;
import com.floreantpos.ui.views.order.RootView;

/**
 *
 * @author mshahriar
 */
public class PrintConfigurationView extends ConfigurationView {

  /**
   * Creates new form PrintConfiguration
   */
  public PrintConfigurationView() {
    initComponents();
  }

  @Override
  public String getName() {
    return com.floreantpos.POSConstants.PRINT_CONFIGURATION;
  }

  @Override
  public void initialize() throws Exception {

    PrintService[] printServices = PrintServiceLookup.lookupPrintServices(null, null);
    cbReceiptPrinterName.setModel(new DefaultComboBoxModel(printServices));
    cbKitchenPrinterName.setModel(new DefaultComboBoxModel(printServices));

    PrintServiceComboRenderer comboRenderer = new PrintServiceComboRenderer();
    cbReceiptPrinterName.setRenderer(comboRenderer);
    cbKitchenPrinterName.setRenderer(comboRenderer);

    setSelectedPrinter(cbReceiptPrinterName, PrintConfig.P_OS_PRINTER_FOR_RECEIPT);
    setSelectedPrinter(cbKitchenPrinterName, PrintConfig.P_OS_PRINTER_FOR_KITCHEN);

    jCheckBox1.setSelected(PrintConfig.isPrintReceiptEnabled());
    jCheckBox2.setSelected(PrintConfig.isPrintKitchenEnabled());

    jCheckBox3.setSelected(PrintConfig.isPrintReceiptWide());
    jCheckBox4.setSelected(PrintConfig.isPrintKitchenWide());

    jCheckBox5.setSelected(PrintConfig.isPrintKitchenManual());

    setInitialized(true);
  }

  private void setSelectedPrinter(JComboBox whichPrinter, String propertyName) {
    PrintService osDefaultPrinter = PrintServiceLookup.lookupDefaultPrintService();

    if (osDefaultPrinter != null) {

      String receiptPrinterName = ApplicationConfig.getString(propertyName, osDefaultPrinter.getName());

      int printerCount = whichPrinter.getItemCount();
      for (int i = 0; i < printerCount; i++) {
        PrintService printService = (PrintService) whichPrinter.getItemAt(i);
        if (printService.getName().equals(receiptPrinterName)) {
          whichPrinter.setSelectedIndex(i);
          return;
        }
      }
    }
  }

  @Override
  public boolean save() throws Exception {
    ApplicationConfig.put(PrintConfig.P_RECEIPT_PRINTER_TYPE, "printer");
    ApplicationConfig.put(PrintConfig.P_KITCHEN_PRINTER_TYPE, "printer");

    PrintService printService = (PrintService) cbReceiptPrinterName.getSelectedItem();
    ApplicationConfig.put(PrintConfig.P_OS_PRINTER_FOR_RECEIPT, printService == null ? null : printService.getName());
    printService = (PrintService) cbKitchenPrinterName.getSelectedItem();
    ApplicationConfig.put(PrintConfig.P_OS_PRINTER_FOR_KITCHEN, printService == null ? null : printService.getName());

    ApplicationConfig.put(PrintConfig.P_PRINT_RECEIPT_ENABLED, jCheckBox1.isSelected());
    ApplicationConfig.put(PrintConfig.P_PRINT_KITCHEN_ENABLED, jCheckBox2.isSelected());

    ApplicationConfig.put(PrintConfig.P_PRINT_RECEIPT_WIDE, jCheckBox3.isSelected());
    ApplicationConfig.put(PrintConfig.P_PRINT_KITCHEN_WIDE, jCheckBox4.isSelected());

    ApplicationConfig.put(PrintConfig.P_PRINT_KITCHEN_MANUAL, jCheckBox5.isSelected());

    RootView.getInstance().getSwitchboadView().updateView();

    return true;
  }

  @SuppressWarnings("unchecked")
  private void initComponents() {

    jPanel1 = new javax.swing.JPanel();
    cbReceiptPrinterName = new javax.swing.JComboBox();
    lblSelectReceiptPrinter = new javax.swing.JLabel();
    jCheckBox1 = new javax.swing.JCheckBox();
    jCheckBox3 = new javax.swing.JCheckBox();
    jPanel2 = new javax.swing.JPanel();
    cbKitchenPrinterName = new javax.swing.JComboBox();
    lblSelectKitchenPrinter = new javax.swing.JLabel();
    jCheckBox2 = new javax.swing.JCheckBox();
    jCheckBox4 = new javax.swing.JCheckBox();
    jCheckBox5 = new javax.swing.JCheckBox();

    jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, " Impressora de Contas ", javax.swing.border.TitledBorder.LEFT, javax.swing.border.TitledBorder.TOP));

    lblSelectReceiptPrinter.setText("Impressora:");

    jCheckBox1.setText("Habilitar");

    jCheckBox3.setText("Formato mais largo");

    javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
    jPanel1.setLayout(jPanel1Layout);
    jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(lblSelectReceiptPrinter)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                    .addComponent(jCheckBox1)
                                    .addGap(18, 18, 18)
                                    .addComponent(jCheckBox3)
                                    .addGap(0, 0, Short.MAX_VALUE))
                            .addComponent(cbReceiptPrinterName, 0, 431, Short.MAX_VALUE))
                    .addContainerGap())
    );
    jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                    .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(lblSelectReceiptPrinter)
                            .addComponent(cbReceiptPrinterName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGap(7, 7, 7)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jCheckBox1)
                            .addComponent(jCheckBox3))
                    .addContainerGap())
    );

    jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(null, " Impressora de Pedidos ", javax.swing.border.TitledBorder.LEFT, javax.swing.border.TitledBorder.TOP));

    lblSelectKitchenPrinter.setText("Impressora:");

    jCheckBox2.setText("Habilitar");

    jCheckBox4.setText("Formato mais largo");

    jCheckBox5.setText("Impressão manual");

    javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
    jPanel2.setLayout(jPanel2Layout);
    jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(lblSelectKitchenPrinter)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                    .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                    .addComponent(jCheckBox2)
                                    .addGap(18, 18, 18)
                                    .addComponent(jCheckBox4)
                                    .addGap(18, 18, 18)
                                    .addComponent(jCheckBox5)
                                    .addGap(0, 0, Short.MAX_VALUE))
                            .addComponent(cbKitchenPrinterName, 0, 431, Short.MAX_VALUE))
                    .addContainerGap())
    );
    jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                    .addContainerGap()
                    .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(lblSelectKitchenPrinter)
                            .addComponent(cbKitchenPrinterName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGap(8, 8, 8)
                    .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jCheckBox2)
                            .addComponent(jCheckBox4)
                            .addComponent(jCheckBox5))
                    .addContainerGap(9, Short.MAX_VALUE))
    );

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
    this.setLayout(layout);
    layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                    .addContainerGap()
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addContainerGap(20, Short.MAX_VALUE))
    );
    layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                    .addGap(42, 42, 42)
                    .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(36, 36, 36)
                    .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(37, Short.MAX_VALUE))
    );
  }

  private javax.swing.JComboBox cbKitchenPrinterName;
  private javax.swing.JComboBox cbReceiptPrinterName;
  private javax.swing.JCheckBox jCheckBox1;
  private javax.swing.JCheckBox jCheckBox2;
  private javax.swing.JCheckBox jCheckBox3;
  private javax.swing.JCheckBox jCheckBox4;
  private javax.swing.JCheckBox jCheckBox5;
  private javax.swing.JPanel jPanel1;
  private javax.swing.JPanel jPanel2;
  private javax.swing.JLabel lblSelectKitchenPrinter;
  private javax.swing.JLabel lblSelectReceiptPrinter;

  private class PrintServiceComboRenderer extends DefaultListCellRenderer {

    @Override
    public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
      JLabel listCellRendererComponent = (JLabel) super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);

      if (PrintServiceLookup.lookupDefaultPrintService() == null) {
        listCellRendererComponent.setText("");
      } else {
        listCellRendererComponent.setText(((PrintService) value).getName());
      }

      return listCellRendererComponent;
    }
  }
}
